package com.jinlejia.bbs.ui.mine.others;


import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.GambitAdapter;
import com.jinlejia.bbs.bean.GambitListBean;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.databinding.BbsFragmentOtherAskBinding;
import com.jinlejia.bbs.logic.LikeFunLogic;
import com.jinlejia.bbs.logic.other.OtherAskLogic;
import com.jinlejia.bbs.ui.reply.BBSReplyDetailActivity;
import com.jinlejia.bbs.widget.mp3.AudioController;
import com.jinlejia.bbs.widget.recycler.PullInterface;

/**
 * A simple {@link Fragment} subclass.
 */
public class BBSOtherAskFragment extends Fragment implements OtherAskLogic.LogicCallBack, OnRefreshLoadListener, PullInterface.LoadMoreListener {


    private OtherAskLogic logic;
    private GambitListBean data = new GambitListBean();
    private BbsFragmentOtherAskBinding binding;
    private LikeFunLogic likeFunLogic;
    private String pin;
    private boolean canToUser;
    public BBSOtherAskFragment() {
    }
    private AudioController audioController;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bbs_fragment_other_ask, container, false);
        binding = DataBindingUtil.bind(view);
        if (logic == null) {
            logic = new OtherAskLogic(getContext(), data, this);
        }
        pin = getArguments().getString("pin");
        canToUser = getArguments().getBoolean("toUser");
        if (likeFunLogic == null) {
            likeFunLogic = new LikeFunLogic(this);
        }
        initView();
        return view;
    }
    private GambitAdapter adapter;
    private void initView() {

        adapter = new GambitAdapter(getContext(),audioController,binding.recyclerView);
        adapter.setGambitOnClickListener(new GambitAdapter.GambitOnClickListener() {
            @Override
            public void onClickTopic(TopicBean bean) {
                Intent intent = new Intent();
                intent.putExtra("topic", bean);
                intent.setClass(getContext(), BBSReplyDetailActivity.class);
                startActivity(intent);
            }
        });
        adapter.setLikeOnClickListener(new GambitAdapter.LikeOnClickListener() {
            @Override
            public void onClickLike(TopicBean topic, final InformationBean information, final int position) {
                likeFunLogic.execute(information.getAnswerCode(), new LikeFunLogic.LogicCallBack() {
                    @Override
                    public void onResult(boolean likeState) {
                        information.setOwnLike(likeState);
                        if (likeState) {
                            information.setLikeCount(information.getLikeCount() + 1);
                        } else {
                            information.setLikeCount(information.getLikeCount() - 1);
                        }
                        adapter.notifyItemChanged(position);
                    }
                });

            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAutoLoadMoreListener(this);
    }

    @Override
    public void onRefresh() {
        logic.refresh(pin);
    }

    @Override
    public void onResult(boolean hasNextPage) {
        adapter.initData(data);
        if (!hasNextPage) {
            binding.recyclerView.setAutoLoadMoreListener(null);
        } else {
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.recyclerView.getAdapter().notifyDataSetChanged();
        if (notifyResultListener!=null){
            notifyResultListener.onNotify();
            notifyResultListener.onNotifyLoadResult();
        }
    }
    private OnNotifyResultListener notifyResultListener;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        audioController = new AudioController(getContext());
        if (context instanceof OnNotifyResultListener){
            notifyResultListener = (OnNotifyResultListener) context;
        }
    }

    @Override
    public void onDetach() {
        if (audioController!=null)
            audioController.release();
        super.onDetach();
        notifyResultListener = null;
    }

    @Override
    public void onLoadMore() {
        if (notifyResultListener!=null){
            notifyResultListener.onNotifyLoadMore();
        }
        logic.load(pin);
    }

}
